package com.china.produce.postProcess;

import com.china.produce.factorys.UserFactoryBean;
import com.china.produce.pojo.User;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * bean
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/1/12
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        /**
         * ConfigurationClassPostProcessor 加载完所有的 beanDefinitions
         */
        System.out.println("beanDefinition num = " + registry.getBeanDefinitionCount());

        RootBeanDefinition beanDefinition = new RootBeanDefinition();

        beanDefinition.setBeanClass(UserFactoryBean.class);

        registry.registerBeanDefinition("user", beanDefinition);

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        System.out.println("beanDefinition num = " + beanFactory.getBeanDefinitionCount());

        System.out.println(beanFactory.getBean("threadPoolConfig"));

        String[] beanNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class);

        System.out.println("beanDefinitionRegistryPostProcessor " + Arrays.asList(beanNames));
    }
}
